home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / iphdr.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  4KB  |  180 lines

  1. /* @(#) $Header: iphdr.c,v 1.5 92/05/14 13:20:09 deyke Exp $ */
  2.  
  3. /* IP header conversion routines
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "ip.h"
  9. #include "internet.h"
  10.  
  11. /* Convert IP header in host format to network mbuf
  12.  * If cflag != 0, take checksum from structure,
  13.  * otherwise compute it automatically.
  14.  */
  15. struct mbuf *
  16. htonip(ip,bp,cflag)
  17. register struct ip *ip;
  18. struct mbuf *bp;
  19. int cflag;
  20. {
  21.     int16 hdr_len;
  22.     register char *cp;
  23.     int16 fl_offs;
  24.  
  25.     hdr_len = IPLEN + ip->optlen;
  26.     if(hdr_len > IPLEN + IP_MAXOPT)
  27.         hdr_len = IPLEN + IP_MAXOPT;
  28.     bp = pushdown(bp,hdr_len);
  29.     cp = bp->data;
  30.  
  31.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  32.     *cp++ = ip->tos;
  33.     cp = put16(cp,ip->length);
  34.     cp = put16(cp,ip->id);
  35.     fl_offs = ip->offset >> 3;
  36.     if(ip->flags.congest)
  37.         fl_offs |= 0x8000;
  38.     if(ip->flags.df)
  39.         fl_offs |= 0x4000;
  40.     if(ip->flags.mf)
  41.         fl_offs |= 0x2000;
  42.  
  43.     cp = put16(cp,fl_offs);
  44.     *cp++ = ip->ttl;
  45.     *cp++ = ip->protocol;
  46.     if(cflag){
  47.         /* Use checksum from host structure */
  48.         cp = put16(cp,ip->checksum);
  49.     } else {
  50.         /* Clear checksum for later recalculation */
  51.         *cp++ = 0;
  52.         *cp++ = 0;
  53.     }
  54.     cp = put32(cp,ip->source);
  55.     cp = put32(cp,ip->dest);
  56.     if(ip->optlen != 0)
  57.         memcpy(cp,ip->options,min(ip->optlen,IP_MAXOPT));
  58.  
  59.     /* If requested, recompute checksum and insert into header */
  60.     if(!cflag)
  61.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  62.  
  63.     return bp;
  64. }
  65. /* Extract an IP header from mbuf */
  66. int
  67. ntohip(ip,bpp)
  68. register struct ip *ip;
  69. struct mbuf **bpp;
  70. {
  71.     int ihl;
  72.     int16 fl_offs;
  73.     char ipbuf[IPLEN];
  74.  
  75.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  76.         return -1;
  77.  
  78.     ip->version = (ipbuf[0] >> 4) & 0xf;
  79.     ip->tos = ipbuf[1];
  80.     ip->length = get16(&ipbuf[2]);
  81.     ip->id = get16(&ipbuf[4]);
  82.     fl_offs = get16(&ipbuf[6]);
  83.     ip->offset = (fl_offs & 0x1fff) << 3;
  84.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  85.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  86.     ip->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  87.     ip->ttl = ipbuf[8];
  88.     ip->protocol = ipbuf[9];
  89.     ip->checksum = get16(&ipbuf[10]);
  90.     ip->source = get32(&ipbuf[12]);
  91.     ip->dest = get32(&ipbuf[16]);
  92.  
  93.     ihl = (ipbuf[0] & 0xf) << 2;
  94.     if(ihl < IPLEN){
  95.         /* Bogus packet; header is too short */
  96.         ip->optlen = 0;
  97.         return -1;
  98.     }
  99.     if ( (ip->optlen = ihl - IPLEN) != 0 ) {
  100.         if ( pullup(bpp,ip->options,ip->optlen) < ip->optlen )
  101.             return -1;
  102.     }
  103.     return ihl;
  104. }
  105. /* Perform end-around-carry adjustment */
  106. int16
  107. eac(sum)
  108. register int32 sum;     /* Carries in high order 16 bits */
  109. {
  110.     register int16 csum;
  111.  
  112.     while((csum = sum >> 16) != 0)
  113.         sum = csum + (sum & 0xffffL);
  114.     return (int16) (sum & 0xffffl); /* Chops to 16 bits */
  115. }
  116. /* Checksum a mbuf chain, with optional pseudo-header */
  117. int16
  118. cksum(ph,m,len)
  119. struct pseudo_header *ph;
  120. register struct mbuf *m;
  121. int16 len;
  122. {
  123.     register int16 cnt, total;
  124.     register int32 sum, csum;
  125.     register char *up;
  126.     int16 csum1;
  127.     int swap = 0;
  128.  
  129.     sum = 0l;
  130.  
  131.     /* Sum pseudo-header, if present */
  132.     if(ph != NULLHEADER){
  133.         sum = hiword(ph->source);
  134.         sum += loword(ph->source);
  135.         sum += hiword(ph->dest);
  136.         sum += loword(ph->dest);
  137.         sum += uchar(ph->protocol);
  138.         sum += ph->length;
  139.     }
  140.     /* Now do each mbuf on the chain */
  141.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  142.         cnt = min(m->cnt, len - total);
  143.         up = (char *)m->data;
  144.         csum = 0;
  145.  
  146.         if(((long)up) & 1){
  147.             /* Handle odd leading byte */
  148.             if(swap)
  149.                 csum = uchar(*up++);
  150.             else
  151.                 csum = (int16)(uchar(*up++) << 8);
  152.             cnt--;
  153.             swap = !swap;
  154.         }
  155.         if(cnt > 1){
  156.             /* Have the primitive checksumming routine do most of
  157.              * the work. At this point, up is guaranteed to be on
  158.              * a short boundary
  159.              */
  160.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  161.             if(swap)
  162.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  163.             csum += csum1;
  164.         }
  165.         /* Handle odd trailing byte */
  166.         if(cnt & 1){
  167.             if(swap)
  168.                 csum += uchar(up[--cnt]);
  169.             else
  170.                 csum += (int16)(uchar(up[--cnt]) << 8);
  171.             swap = !swap;
  172.         }
  173.         sum += csum;
  174.         total += m->cnt;
  175.     }
  176.     /* Do final end-around carry, complement and return */
  177.     return (int16)(~eac(sum) & 0xffff);
  178. }
  179.  
  180.